package com.haois.ws.common.util;


import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;
import java.util.Random;

/**
 * Created by RuiHaoZ on 2018/3/22 19:05
 */
public class StrUtil {

    public static void main(String[] args) {
        System.out.println(getRandom(5));
    }

    /**
     * 获取随机数
     *
     * @param size 随机数长度
     * @return 长度为size的随机数
     */
    public static String getRandom(int size) {
        Random random = new Random();
        String tem = "";
        for (int i = 0; i < size; i++) {
            tem += "0";
        }
        String randomStr = tem + String.valueOf(random.nextInt((int) Math.pow(10, size)));
        randomStr = randomStr.substring(randomStr.length() - size);
        return randomStr;
    }

    /**
     * @param size
     * @param objects
     * @return
     */
    public static String getRandom(int size, String... objects) {
        String randomStr = "";
        for (int i = 0; i < objects.length; i++) {
            randomStr += objects[i] + "-";
        }
        randomStr += getRandom(size);
        return randomStr;
    }

    /**
     * 获取随机数
     *
     * @param prefix 前缀
     * @return prefix-20180324-12345
     */

    public static String getFileRandomStr(String prefix) {
        String randomStr = getRandom(2, prefix, DateUtil.getyMdHmsSS_());
        return randomStr;
    }

    public static boolean isEmpty(Object obj) {
        if (obj == null) {
            return true;
        }
        if (obj instanceof String) {
            return "".equals(((String) obj).trim());
        }
        if (obj instanceof Collection) {
            return ((Collection<?>) obj).isEmpty();
        }
        if (obj instanceof Map) {
            return ((Map<?, ?>) obj).isEmpty();
        }
        if (obj.getClass().isArray()) {
            return Array.getLength(obj) == 0;
        }
        return false;
    }

    public static boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }

    public static boolean hasEmpty(Object obj, Object... objects) {
        if (isEmpty(obj) || isEmpty(objects)) {
            return true;
        }
        for (Object object : objects) {
            if (isEmpty(object)) {
                return true;
            }
        }
        return false;
    }

    public static boolean hasNotEmpty(Object obj, Object... objects) {
        if (isNotEmpty(obj)) {
            return true;
        }
        if (isNotEmpty(objects)) {
            for (Object object : objects) {
                if (isNotEmpty(object)) {
                    return true;
                }
            }
        }
        return false;
    }

    public static String getOS() {
        String os = System.getProperty("os.name");
        if (os.indexOf("indows") < 0) {
            return "/home/file/phone/excel/up/";
        } else {
            return "D:\\MobileGroup\\20180215\\";
        }
    }

    public static String getFileSuffix(String fileName) {
        try {
            return fileName.substring(fileName.lastIndexOf(".") + 1);
        } catch (Exception e) {
            e.printStackTrace();
            return "xxx";
        }

    }

    public static String getFilePrefix(String fileName) {
        try {
            return fileName.substring(0, fileName.lastIndexOf(".") - 1);
        } catch (Exception e) {
            e.printStackTrace();
            return "截取出错";
        }

    }

    /**判断是否为文件名
     * @param param \ / : * ? " < > |
     * @return
     */
    public static boolean isFileName(String param){
        String code3Reg ="[\\\\ /:*?\"<>|]+";
        if(!param.matches(code3Reg)){
            return true;
        }
        return false;
    }

    /**判断是否手机号码
     * @param param \ / : * ? " < > |
     * @return
     */
    public static boolean isPhone(String param){
        String code3Reg ="^1[3|4|5|6|7|8|9][0-9]{9}$";
        if(param.matches(code3Reg)){
            return true;
        }
        return false;
    }
}
